package com.fuqi.android.zhongcai.business.shuig.view.adapter;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.base.request.result.bean.VisitBean;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AnswerQuestionAdapter extends BaseAdapter {
	private List<VisitBean> listBean = new ArrayList<VisitBean>();
	private Context context;
//	private List<GridView> mgvsa;
	private Map<String,View> mgvs;

	public AnswerQuestionAdapter(Context context,List<VisitBean> listBean) {
		this.context = context;
		this.listBean = listBean;
		mgvs = new HashMap<String,View>();
	}
	public void setDate(List<VisitBean> listBean) {
		this.listBean = listBean;
		notifyDataSetChanged();
	}



	@Override
	public int getCount() {
		return listBean.size();
	}

	@Override
	public VisitBean getItem(int position) {
		return listBean.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.item_huifang, null);
			viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
			viewHolder.gvItem = (GridView) convertView.findViewById(R.id.gv_item);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.gvItem.setTag(getItem(position));
		if(!mgvs.containsKey(getItem(position).getAid())){
			mgvs.put(getItem(position).getAid(),viewHolder.gvItem);
		}
		viewHolder.gvItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				VisitBean v = (VisitBean) ((View)view.getParent()).getTag();
				mgvs.put(v.getAid(),(View)view.getParent());
			}
		});

		final List<VisitBean> datass = getItem(position).getRls();
		AnswerAdapter mTypeAdapter = new AnswerAdapter(context,datass);
		viewHolder.tvTitle.setText(getItem(position).getName());
		viewHolder.gvItem.setNumColumns(getNumColumns(datass));
		viewHolder.gvItem.setAdapter(mTypeAdapter);
		return convertView;
	}

	private class ViewHolder{
        TextView tvTitle;
        GridView gvItem;
    }
	public JsonArray getItem(){
		JsonArray jsonarray = new JsonArray();

		for (int j = 0; j < listBean.size(); j++) {
			JsonObject json = new JsonObject();
			VisitBean question = listBean.get(j);
			GridView gv = (GridView)mgvs.get(question.getAid());
			if(gv != null) {
				SparseBooleanArray poss = gv.getCheckedItemPositions();
				for (int i = 0; i < poss.size(); i++) {
					int key = poss.keyAt(i);
					if (poss.get(key)) {
						VisitBean answer = ((AnswerAdapter) gv.getAdapter()).getDate().get(key);
						json.addProperty("QUESTION_CODE", question.getBaseCode());
						json.addProperty("RESULT", answer.getBaseCode());
						json.addProperty("ORDY_BY", question.getOrder());

						jsonarray.add(json);
					}
				}
			}
		}
		return jsonarray;
	}

	private int getNumColumns(List<VisitBean> datass){
		for (VisitBean bean : datass) {
			if(bean.getName().length()>8)
				return 1;
		}
		return 2;
	}
}
